#include "ExplosionObject.h"

int ExplosionObject::exp_name = 0;
bool ExplosionObject::compiled = false;

ExplosionObject::ExplosionObject(){}

ExplosionObject::~ExplosionObject(){}


void ExplosionObject::LoadLists()
{
//disegna l'esplosione
exp_name = glGenLists(1);
glNewList(exp_name,GL_COMPILE);
         glColor4f(0.50f, 1.0f, 1.0f, 0.7);     
        glBlendFunc(GL_SRC_ALPHA,GL_ONE);       
        glEnable(GL_BLEND);   
        glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D,state->th->texNames[TEX_FUOCO]);
            gluSphere(state->quad_obj,radius,20,20);
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_BLEND);
 glEndList();
compiled = true;
}

void ExplosionObject::DoIdle()
{
 if  (scale  < 3 ) scale += 0.1,rang = rand()%360;
 else 
 { 
   /* if(rand()%2 == 1)*/Generate_Smoke(pos.x,pos.y,pos.z);
    visible = false;
 }
  
}

void ExplosionObject::Init()
{
    visible = true;
    scale = 1;
    rang = 0;
    radius = (rand() % 10) + 15;
     if (!compiled) LoadLists();
}




void ExplosionObject::Draw()
{
  
 glPushMatrix();
        glTranslatef(pos.x,pos.y-35,pos.z);
        glScalef(scale,scale,scale);
        glRotatef(rang,0,1,0);
        glCallList(exp_name);
glPopMatrix();

}


void ExplosionObject::Generate_Smoke(float x,float y,float z)
{
   SmokeObject * s = new SmokeObject(state);
   s->Init();
   s->setPos(x,y,z);
   s->setScale(4);
   s->setOffset(25);
   
    bool flag = false;
    for (int i = 0;(!flag && i <= state->smoke_counter);i++)
          if (state->smokes[i] == NULL) state->smokes[i] = s,flag = true;
   
   state->smoke_counter++;
}
 
